package name.gauravmadan.mdg.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import name.gauravmadan.mdg.pim.Class;
import name.gauravmadan.mdg.pim.Component;
import name.gauravmadan.mdg.service.ModelDataService;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class ClassListController extends MultiActionController{
	private ModelDataService service;
	
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = null;
		String parentComponentId = request.getParameter("parentComponentId");
		if (parentComponentId != null && !"".equals(parentComponentId)) {
			List<Class> classList = service.getClassListByCompId(new Integer(parentComponentId));
			mav = new ModelAndView("class-list");
			mav.addObject("classList", classList);
			
			// To build the link for Component List, we need to fetch the application id corresponding to the parentComponentId
			Component component = service.getComponentById(new Integer(parentComponentId));
			mav.addObject("parentApplicationId",component.getParentApplicationId());
		}
		else {
			// Return error here
		}
		return mav;
	}

	public ModelAndView showFormNew(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = null;
		Class clazz = new Class();
		String parentComponentId = request.getParameter("parentComponentId");
		if (parentComponentId != null && !"".equals(parentComponentId)){
			clazz.setParentComponentId(new Integer(parentComponentId));
			mav = new ModelAndView("class-form");
			mav.addObject("clazz", clazz);
		}
		else {
			// Return error here
		}
		return mav;
	}
	public ModelAndView showFormEdit(HttpServletRequest request, HttpServletResponse response)
	{
		String selectedClassId = request.getParameter("selectedClassId");
		// Load Component Object based on selected class id
		ModelAndView mav = new ModelAndView("class-form");
		Class clazz = null;
		if (selectedClassId != null && !"".equals(selectedClassId)) {
			clazz = service.getClassById(new Integer(selectedClassId));
		}
		else {
			// Return error here
		}
		mav.addObject("clazz", clazz);
		return mav;
	}
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response)
	{
		String selectedClassId = request.getParameter("selectedClassId");
		if (selectedClassId != null && !"".equals(selectedClassId)) {
			service.deleteClassById(new Integer(selectedClassId));
		}
//		ModelAndView mav = null;
//		String parentComponentId = request.getParameter("parentComponentId");
//		if (parentComponentId != null && !"".equals(parentComponentId)){
//			List<Class> classList = service.getClassListByCompId(new Integer(parentComponentId));
//			mav = new ModelAndView("class-list");
//			mav.addObject("classList", classList);
//		}else {
//			// Return error here
//		}
//		return mav;
		ModelAndView mav = new ModelAndView("redirect:/class-list.html?method=list");
		mav.addObject("parentComponentId", request.getParameter("parentComponentId"));
		return mav;
	}
	public ModelAndView showAttributes(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("redirect:/attribute-list.html?method=list");
		mav.addObject("parentClassId", request.getParameter("selectedClassId"));
		return mav;
	}
	
	public ModelAndView showAssociations(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("redirect:/association-list.html?method=list");
		mav.addObject("containerClassId", request.getParameter("selectedClassId"));
		return mav;
	}
	
	public ModelDataService getService() {
		return service;
	}
	public void setService(ModelDataService service) {
		this.service = service;
	}
}
